//
//  outgoAppViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/8.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 确认配送提交
class outgoAppViewController: warehouseApiViewController {
    
    @IBOutlet weak var cdh: UILabel! // 仓单号
    @IBOutlet weak var thr: UITextField! // 提货人
    @IBOutlet weak var cardID: UITextField! // 身份证号
    //@IBOutlet weak var ckfs: UILabel!   // 出库方式
    //@IBOutlet weak var address: UITextView! //收货地址
    //@IBOutlet weak var yb: UITextField! // 邮编
    //@IBOutlet weak var mobile: UITextField! // 电话
    @IBOutlet weak var sq: UIButton! // 申请
    //@IBOutlet weak var cz: UIButton!    //重置
    
    @IBOutlet weak var ztBtn: DLRadioButton!
    @IBOutlet weak var psBtn: DLRadioButton!
    var sta : String = "1"
    
    var outApp : outgoApp?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.cdh.text = self.outApp?.BII
        
        
            //BtnSegmentClick.addTarget(self, action: #selector(SegBtnClick), for: .touchUpInside)

        
        
        
        // Do any additional setup after loading the view.
        showInfo()
    }
    


}


extension outgoAppViewController {
    private func showInfo() {
            ztBtn.addTarget(self, action: #selector(mrBtnClick), for: .touchUpInside)
            psBtn.addTarget(self, action: #selector(mcBtnClick), for: .touchUpInside)
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User

Thread.sleep(forTimeInterval: 0.05)
           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {

               if !self.isLogin {
                if #available(iOS 13.0, *) {
                    self._alertVisitorView()
                }
                return
               } else {
                Thread.sleep(forTimeInterval: 0.05)
                   // 2.设置导航栏内容
                    //self.tableShouInfo()
                
                self.setupView()
               }
            }
       }
    
    @objc private func mrBtnClick() {
        self.sta = "1"
    }
    
    @objc private func mcBtnClick() {
        self.sta = "2"
    }
}
extension outgoAppViewController {
    @objc func setupView() {
   print("111")
        // 监听登陆按钮
        sq.addTarget(self, action: #selector(sqTjBtnClick), for: .touchUpInside)

        // 返回上一页按钮
        //cz.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
    }
    
    // 绑定登陆按钮
    @objc private func sqTjBtnClick()
    {
        //print("1")
        if self.thr.text == "" {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "提货人不能为空")
            }
            return
        }
        
        if self.cardID.text == ""   {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "身份证号不能为空")
            }
            return
        }
        /*
        guard self.address.text != "" else {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "收货地址不能为空")
            }
            return
        }
        
        guard self.yb.text != "" else {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "邮编不能为空")
            }
            return
        }
        
        guard self.mobile.text != "" else {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "电话不能为空")
            }
            return
        }
        */
        
        
        
        let bii : String = (self.outApp?.BII)!
        let thr : String = self.thr.text!
        let cardID : String = self.cardID.text!
        //let address : String = self.address.text!
        //let yb : String = self.yb.text!
        //let mobile : String = self.mobile.text!
        
        let str = [user?.U!,user?.RETCODE!,bii ,thr, cardID, self.sta, "address","yb", "mobile"]
    print(str)
        
        NetworkTools.share.out_bill(code: str as Array<Any>) { (result, error) in
            
            if error != nil {
                print("no no no")
                return
            }
            

            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            
            print(resultArray)
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let arr = REP["RESULT"] as? [String : AnyObject]   {
                    let code : Int = Int(arr["RETCODE"] as! String)!
                    if(Int(truncating: NSNumber(value: code)) > 0) {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "请求成功")
                        }
                    } else {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "请求失败")
                        }
                    }
                }
            }
          
        }
    }
}
